﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="Email.aspx.cs" Inherits="ankietyzacja.Email" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
Korespondencja
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
    Korespondencja
</h2>

    <table border="0" cellpadding="3" cellspacing="3" width="100%">
    <tr>
    <td colspan="2">
                <asp:Label ID="KomunikatLabel" runat="server" Text="" CssClass="failureNotification"></asp:Label>
    </td></tr>
        <tr>
            <td width="60%" valign="top" >
                <asp:DetailsView ID="EmailDetailsView" runat="server" Height="50px" Width="100%" AutoGenerateRows="False"
                    DataKeyNames="EMAIL_ID" DataSourceID="EmailSqlDataSource" GridLines="None">
                    <Fields>
                        <asp:TemplateField HeaderText="Temat" SortExpression="TEMAT">
                            <EditItemTemplate>
                            <asp:HiddenField runat="server" ID="DataWyslaniaHiddenField" Value='<%# Bind("DATAWYSLANIA") %>' />
                                <asp:TextBox ID="TematTextBox" runat="server" Text='<%# Bind("TEMAT") %>' TextMode="MultiLine"
                                    CssClass="textEntry" Width="100%" Rows="2"></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                            <asp:HiddenField runat="server" ID="DataWyslaniaHiddenField" Value='<%# Bind("DATAWYSLANIA") %>' />
                                <asp:Label ID="TematLabel" runat="server" Text='<%# Bind("TEMAT") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle Width="10%" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Treść" SortExpression="TRESC">
                            <EditItemTemplate>
                                <asp:TextBox ID="TrescTextBox" runat="server" Text='<%# Bind("TRESC") %>' TextMode="MultiLine"
                                    CssClass="textEntry" Width="100%" Rows="20"> </asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="TrescLabel" runat="server" Text='<%# Bind("TRESC") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle Width="10%" />
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:LinkButton ID="UpdateLinkButton" runat="server" CausesValidation="True" 
                                    CommandName="Update" Text="Zapisz"></asp:LinkButton>
                                &nbsp;<asp:LinkButton ID="AnulujLinkButton" runat="server" CausesValidation="False" 
                                    CommandName="Cancel" Text="Anuluj"></asp:LinkButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="EditLinkButton" runat="server" CausesValidation="False" 
                                    CommandName="Edit" Text="Edytuj"></asp:LinkButton>
                            </ItemTemplate>
                            <ControlStyle CssClass="commandLink" />
                            <ItemStyle CssClass="commandLink" />
                        </asp:TemplateField>
                        
                    </Fields>
                </asp:DetailsView>
            </td>
            <td align="center" valign="top" rowspan="2" >
                <asp:Button ID="DodajAdresatowButton" runat="server" Text="Dodaj adresatów" Width="133px" 
                    onclick="DodajAdresatowButton_Click" />
                <asp:GridView ID="AdresaciGridView" runat="server" Width="100%" AllowPaging="True" 
                    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="UZYTKOWNIK_ID" 
                    DataSourceID="AdresaciSqlDataSource" PageSize="30" GridLines="None">
                    <Columns>
                        <asp:BoundField DataField="IMIE" HeaderText="Imię" SortExpression="IMIE" />
                        <asp:BoundField DataField="NAZWISKO" HeaderText="Nazwisko" 
                            SortExpression="NAZWISKO" />
                        <asp:BoundField DataField="NUMERALBUMU" HeaderText="Album" 
                            SortExpression="NUMERALBUMU" />
                        <asp:BoundField DataField="ROKUKONCZENIASTUDIOW" HeaderText="Koniec studiów" 
                            SortExpression="ROKUKONCZENIASTUDIOW" />
                        <asp:CommandField DeleteText="Usuń" ShowDeleteButton="True" >
                        <ControlStyle CssClass="commandLink" />
                        </asp:CommandField>
                    </Columns>
                    <PagerSettings Position="TopAndBottom" />
                    <PagerStyle HorizontalAlign="Center" />
                </asp:GridView>
            </td>
        </tr>
        <tr>
        <td valign="bottom" align="center">
                <asp:GridView ID="AnkietyGridView" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="WYSLANAANKIETA_ID" DataSourceID="AnkietySqlDataSource" 
                    Caption="&lt;div class=&quot;boldGreen&quot;&gt;Ankiety dołączone do e-maila&lt;/div&gt;" 
                    CaptionAlign="Top" Width="100%" ShowHeaderWhenEmpty="True" 
                    onrowdeleted="AnkietyGridView_RowDeleted" GridLines="None">
                    <Columns>
                        <asp:BoundField DataField="NAZWAANKIETY" HeaderText="Nazwa" />
                        <asp:BoundField DataField="CZASZYCIA" HeaderText="Ważna do" />
                        <asp:CommandField DeleteText="Usuń" ShowDeleteButton="True" >
                        <ControlStyle CssClass="commandLink" />
                        <ItemStyle CssClass="commandLink" />
                        </asp:CommandField>
                    </Columns>
                    <HeaderStyle CssClass="bold" />
                </asp:GridView>

            <asp:Label ID="DostepneAnkietyLabel" runat="server" Text="Dostępne ankiety" 
                    CssClass="boldGreen"></asp:Label>
                <asp:DropDownList ID="DostepneAnkietyDropDownList" runat="server" 
                    DataSourceID="DostepneAnkietySqlDataSource" DataTextField="NAZWAANKIETY" 
                    DataValueField="ANKIETA_ID" Height="20px" Width="477px">
                </asp:DropDownList>
                <asp:Button ID="DodajAnkieteButton" runat="server" 
                    onclick="DodajAnkieteButton_Click" Text="Dodaj" />

                

        </td></tr>
        <tr>
        <td align="center" colspan="2">
          <asp:Button ID="SendButton" runat="server" Text="Wyślij" 
                onclientclick="return confirm(&quot;Czy na pewno wysłać e-mail?&quot;)" 
                Width="120px" onclick="SendButton_Click" />
          <asp:Button ID="UtworzKopieButton" runat="server" Text="Utwórz kopię" 
                Width="120px" onclick="UtworzKopieButton_Click" />
        </td></tr>
    </table>
    <asp:SqlDataSource ID="EmailSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ankietyzacjaConnectionString %>"
        SelectCommand="SELECT * FROM [EMAIL] WHERE ([EMAIL_ID] = @EMAIL_ID)" 
        UpdateCommand="UPDATE [EMAIL] SET [TEMAT] = @TEMAT, [TRESC] = @TRESC WHERE [EMAIL_ID] = @EMAIL_ID">
        <SelectParameters>
            <asp:Parameter Name="EMAIL_ID" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="TEMAT" Type="String" />
            <asp:Parameter Name="TRESC" Type="String" />
            <asp:Parameter Name="EMAIL_ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
                <asp:SqlDataSource ID="DostepneAnkietySqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ankietyzacjaConnectionString %>" 
                    SelectCommand="SELECT * 
                                     FROM ANKIETA a
                                    WHERE CZYWZORZEC = @CZYWZORZEC
                                      AND CZASZYCIA &gt; getDate()
                                      AND NOT EXISTS (
                                      SELECT 1
                                        FROM WYSLANAANKIETA w 
                                       WHERE w.ANKIETA_ID = a.ANKIETA_ID
                                         AND w.EMAIL_ID = @EMAIL_ID
                                         )
                                 ORDER BY ANKIETA_ID">
                    <SelectParameters>
                        <asp:Parameter DefaultValue="0" Name="CZYWZORZEC" Type="Int32" />
                        <asp:Parameter Name="EMAIL_ID" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <asp:SqlDataSource ID="AdresaciSqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ankietyzacjaConnectionString %>" 
                    SelectCommand="SELECT UZYTKOWNIK.UZYTKOWNIK_ID, UZYTKOWNIK.NUMERALBUMU, UZYTKOWNIK.IMIE, UZYTKOWNIK.NAZWISKO, UZYTKOWNIK.ROKUKONCZENIASTUDIOW 
                                     FROM UZYTKOWNIK INNER JOIN UZYTKOWNIK_x_EMAIL 
                                        ON UZYTKOWNIK.UZYTKOWNIK_ID = UZYTKOWNIK_x_EMAIL.UZYTKOWNIK_ID
                                    WHERE EMAIL_ID = @EMAIL_ID 
                                 ORDER BY UZYTKOWNIK.UZYTKOWNIK_ID"
                    DeleteCommand="DELETE
                                     FROM UZYTKOWNIK_x_EMAIL 
                                    WHERE EMAIL_ID = @EMAIL_ID 
                                      AND UZYTKOWNIK_ID = @UZYTKOWNIK_ID ">
                  <SelectParameters>
                    <asp:Parameter Name="EMAIL_ID" Type="Int32" />
                  </SelectParameters>
                  <DeleteParameters>
                    <asp:Parameter Name="EMAIL_ID" Type="Int32" />
                    <asp:Parameter Name="UZYTKOWNIK_ID" Type="Object" />
                  </DeleteParameters>
                </asp:SqlDataSource>
                <asp:SqlDataSource ID="AnkietySqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ankietyzacjaConnectionString %>" 
                    DeleteCommand="DELETE FROM WYSLANAANKIETA WHERE WYSLANAANKIETA_ID = @WYSLANAANKIETA_ID" 
                    SelectCommand="SELECT a.NAZWAANKIETY, a.ANKIETA_ID, a.CZASZYCIA, w.WYSLANAANKIETA_ID FROM WYSLANAANKIETA w, ANKIETA a WHERE w.ANKIETA_ID = a.ANKIETA_ID AND (EMAIL_ID =@EMAIL_ID) ORDER BY w.ANKIETA_ID" 
                    >
                    <DeleteParameters>
                        <asp:Parameter Name="WYSLANAANKIETA_ID" Type="Int32" />
                    </DeleteParameters>
                    <SelectParameters>
                        <asp:Parameter Name="EMAIL_ID" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>

</asp:Content>
